import {fileURLToPath, URL} from 'node:url';

import {defineConfig, loadEnv} from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import vueDevTools from 'vite-plugin-vue-devtools';
import viteCompression from 'vite-plugin-compression';
import {visualizer} from 'rollup-plugin-visualizer';
// https://vite.dev/config/
export default defineConfig(({mode}) => {
    const env = loadEnv(mode, process.cwd());
    console.log(env);
    return {
        server: {
            port: 9080,
            host: '0.0.0.0',
            open: true,
            proxy: {
                '/api': {
                    target: 'https://cnodejs.org/api/v1',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, '')
                }
            }
        },
        envPrefix: 'VITE_',
        build: {
            // outDir: env.VITE_OUTPUT_DIR,
            // assetsDir: 'assets-aa',
            cssCodeSplit: true,
            assetsInlineLimit: 4096,
            rollupOptions: {
                output: {
                    manualChunks: {
                        lodash: ['lodash'],
                        echarts: ['echarts'],
                        dayjs: ['dayjs'],
                        axios: ['axios'],
                        'element-plus': ['element-plus'],
                        vue: ['vue', 'vue-router', 'pinia']
                    }
                }
            }
            // chunkSizeWarningLimit: 1500 * 1024
        },
        plugins: [
            vue(),
            vueJsx(),
            vueDevTools(),
            // gzip压缩 生产环境生成 .gz 文件
            viteCompression({
                verbose: true,
                disable: false,
                threshold: 10240,
                algorithm: 'gzip',
                ext: '.gz'
            }),
            // brotli压缩 生产环境生成.br 文件
            viteCompression({
                verbose: true,
                disable: false,
                threshold: 10240,
                algorithm: 'brotliCompress',
                ext: '.br'
            }),
            visualizer({
                open: true,
                gzipSize: true,
                brotliSize: true,
                template: 'network'
            })
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            }
        }
    };
});
